package com.eightywork.life.activity;

import java.util.ArrayList;

import java.util.List;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.adapter.MessageListAdapter;
import com.eightywork.life.fragment.BaseFragment;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Community;
import com.eightywork.life.model.Space;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.DataService;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.AndroidUtil;
import com.eightywork.life.util.MyHttpException;
import com.eightywork.life.widget.ActionsContentView;
import com.eightywork.life.widget.RTPullListView;
import com.eightywork.life.widget.RTPullListView.OnRefreshListener;
import com.eightywork.life.widget.RTPullListView.OnScrollStopAndEndListener;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.AbsListView.OnScrollListener;
import android.widget.TextView;

public class MessageListActivity extends BaseActivity implements
		OnClickListener {
	private ImageButton backButton;
	private RTPullListView listView;
	private MessageListAdapter adapter;
	public static List<Community> list = new ArrayList<Community>();
	private int page;
	private AsyncTask<?, ?, ?> task, space;
	private View footView;
	private boolean isLoading, isRefresh;
	private String uid;
	private TextView headerTitle;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_message_list);
		initView();
		initData();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}


	@Override
	public void onDestroy() {
		if (task != null)
			task.cancel(true);
		super.onDestroy();
	}

	public void initView() {
		backButton = (ImageButton) findViewById(R.id.button_left);
		headerTitle = (TextView) findViewById(R.id.header_title);
		listView = (RTPullListView) findViewById(R.id.listView1);
		footView = (ViewGroup) getLayoutInflater().inflate(
				R.layout.item_footer, null);
		backButton.setOnClickListener(this);
	}

	private void initData() {
		uid = getIntent().getStringExtra("uid");
		initHeader(getIntent().getStringExtra("name"));
		adapter = new MessageListAdapter(context, list);
		listView.setAdapter(adapter);
		listView.setonRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				page = 0;
				isRefresh = true;
				getXiaoqu();
			}
		});
		listView.setOnScrollStopAndEndListener(new OnScrollStopAndEndListener() {
			public void onEndAndStopScroll() {
				if (!isLoading) {
					getXiaoqu();
				}
			}
		});
		listView.goLoading();
	}

	private void getXiaoqu() {
		task = new ListTask().execute();
	}

	class ListTask extends AsyncTask<Void, Void, Void> {
		List<Community> aList;

		@Override
		protected void onPreExecute() {
			isLoading = true;
			page++;
			if (!isRefresh) {
				listView.addFooterView(footView);
			}
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			try {
				aList = SpaceService.home("", "", page + "", "", "", uid);
			} catch (MyHttpException e) {
				showMessage(e.getErrorMessage());
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			if (result == null) {
				if(isRefresh){
					isRefresh = false;
					list.clear();
					listView.onRefreshComplete();
				}else{
					listView.removeFooterView(footView);
				}
				if (aList != null)
					list.addAll(aList);
				adapter.notifyDataSetChanged();
			}
			isLoading = false;
			super.onPostExecute(result);
		}
	}

	@Override
	public void onClick(View v) {
		if (backButton == v) {
			finish();
		}
	}

}
